package com.jxdinfo.hussar.activitidemo.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jxdinfo.hussar.activitidemo.bean.TdbYjMBookTriantypeCode;
import com.jxdinfo.hussar.activitidemo.bean.TdbYjMMbook;
import com.jxdinfo.hussar.activitidemo.bean.TdbYjMMbookDetail;
import com.jxdinfo.hussar.activitidemo.dao.TdbYjMBookTriantypeCodeDao;
import com.jxdinfo.hussar.activitidemo.dao.TdbYjMMbookDao;
import com.jxdinfo.hussar.activitidemo.dao.TdbYjMMbookDetailDao;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-03-22
 */
@Service
public class TdbYjMMbookServiceImpl extends ServiceImpl<TdbYjMMbookDao, TdbYjMMbook> implements ITdbYjMMbookService {
	@Autowired
	TdbYjMMbookDao dao;
	@Autowired
	TdbYjMMbookDetailDao detailDao;
	@Autowired
	TdbYjMBookTriantypeCodeDao typeCodeDao;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Object add(TdbYjMMbook mBook) {
		dao.add(mBook);
		String mBookId = mBook.getsMbookId();
		String triantypeCode = mBook.getTriantypeCode();
		String sTraintype = mBook.getsTraintype();
		List<TdbYjMBookTriantypeCode> typeList = new ArrayList<TdbYjMBookTriantypeCode>();
		if (StringUtils.isNotBlank(triantypeCode) && StringUtils.isNotBlank(sTraintype)) {
			sTraintype = sTraintype.replace("，", ",");
			String codes[] = triantypeCode.split("~");
			String types[] = sTraintype.split(",");
			List<String> strs = new ArrayList<String>();
			if (codes.length > 1) {
				int startNum = Integer.parseInt(codes[0]);
				int endNum = Integer.parseInt(codes[1]);
				for (int i = startNum; i <= endNum; i++) {
					strs.add("" + i);
				}
			} else {
				strs.add(codes[0]);
			}
			for (String code : strs) {
				for (String type : types) {
					typeList.add(new TdbYjMBookTriantypeCode(mBookId, type, code));

				}
			}
			typeCodeDao.addList(typeList);
		}
		String detailStr = mBook.getTdbYjMMbookDetailsStr();
		if (StringUtils.isNotBlank(detailStr)) {
			List<TdbYjMMbookDetail> detailList = (List<TdbYjMMbookDetail>) JSONArray.parseArray(detailStr,
					TdbYjMMbookDetail.class);
			for (int i=0;i<detailList.size();i++) {
				if(detailList.get(i)!=null){
					detailList.get(i).setsMbookId(mBook.getsMbookId());
				}
			}
			List<TdbYjMMbookDetail> detailList1 = new ArrayList<TdbYjMMbookDetail>();
			for(int i=0;i<detailList.size();i++){
				if(detailList.get(i)!=null){
					detailList1.add(detailList.get(i));
				}
			}
			if(detailList1!=null&&detailList1.size()>0){
				detailDao.addList(detailList1);
			}
		}
		return null;
	}

	@Override
	public Page<TdbYjMMbook> bookSearch(Page<TdbYjMMbook> page, TdbYjMMbook info) {
		// String triantypeCode = info.getTriantypeCode();
		// if (StringUtils.isNotBlank(triantypeCode)) {
		// // 按故障代码查询
		// String codes[] = triantypeCode.split("~");
		// String code = "";
		// if (codes.length > 1) {
		// int startNum = Integer.parseInt(codes[0]);
		// int endNum = Integer.parseInt(codes[1]);
		// for (int i = startNum; i <= endNum; i++) {
		// code = code + i;
		// }
		// info.setTriantypeCode(code);
		// }
		// }
		List<TdbYjMMbook> list = dao.bookSearch(page, info);
		page.setRecords(list);
		page.setTotal(list.size());
		page.getPages();
		return page;
	}

	@Override
	public Page<TdbYjMMbook> list(Page<TdbYjMMbook> page, TdbYjMMbook info) {
		List<TdbYjMMbook> list = dao.list(page, info);
		page.setRecords(list);
		String count = dao.selectCountByCondition(info);
		page.setTotal(Integer.valueOf(count));
		return page;
	}

	@Override
	public void delete(TdbYjMMbook mBook) {
		dao.delete(mBook);

	}

	@Override
	public void updateStatus(TdbYjMMbook mBook) {
		dao.updateStatus(mBook);
	}

	@Override
	public TdbYjMMbook selectById(String tdbYjMMbookId) {
		TdbYjMMbook info = dao.selectById(tdbYjMMbookId);
		// List<TdbYjMMbookDetail> detailList =
		// detailDao.listByMbookId(tdbYjMMbookId);
		// if (detailList.size() > 0) {
		// info.setTdbYjMMbookDetail(detailList);
		// }
		return info;
	}

	@Override
	public Page<TdbYjMMbookDetail> detailList(Page<TdbYjMMbookDetail> page, TdbYjMMbookDetail infoDetail) {
		List<TdbYjMMbookDetail> detailList = detailDao.listByMbookId(page, infoDetail);
		page.setRecords(detailList);
		page.setTotal(detailList.size());
		page.getPages();
		return page;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer updateInfo(TdbYjMMbook mBook) {
		dao.updateInfo(mBook);
		String mBookId = mBook.getsMbookId();
		String triantypeCode = mBook.getTriantypeCode();
		String sTraintype = mBook.getsTraintype();
		List<TdbYjMBookTriantypeCode> typeList = new ArrayList<TdbYjMBookTriantypeCode>();
		if (StringUtils.isNotBlank(triantypeCode) && StringUtils.isNotBlank(sTraintype)) {
			sTraintype = sTraintype.replace("，", ",");
			triantypeCode = triantypeCode.replace("，", ",");
			String codes[] = triantypeCode.split(",");
			String types[] = sTraintype.split(",");
			for (String code : codes) {
				for (String type : types) {
					typeList.add(new TdbYjMBookTriantypeCode(mBookId, type, code));
				}
			}
			typeCodeDao.deleteBySmbookId(mBookId);
			typeCodeDao.addList(typeList);
		}
		String detailStr = mBook.getTdbYjMMbookDetailsStr();
		if (StringUtils.isNotBlank(detailStr)) {
			List<TdbYjMMbookDetail> detailList = (List<TdbYjMMbookDetail>) JSONArray.parseArray(detailStr,
					TdbYjMMbookDetail.class);
			for (int i=0;i<detailList.size();i++) {
				if(detailList.get(i)!=null){
					detailList.get(i).setsMbookId(mBook.getsMbookId());
				}
			}
			detailDao.deleteBySmbookId(mBookId);
			List<TdbYjMMbookDetail> detailList1 = new ArrayList<TdbYjMMbookDetail>();
			for(int i=0;i<detailList.size();i++){
				if(detailList.get(i)!=null){
					detailList1.add(detailList.get(i));
				}
			}
			if(detailList1!=null&&detailList1.size()>0){	
				detailDao.addList(detailList1);
			}
		}
		return 1;
	}

}
